package com.yuncheng.oaApi.service.impl;

import cn.hutool.core.date.DateUtil;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.entity.OaFjzykEntity;
import com.yuncheng.oaApi.mapper.OaFjzykEntityMapper;
import com.yuncheng.oaApi.service.IOaFjzykEntityService;
import com.yuncheng.oaApi.utils.OaLocalFileUploadUtils;
import com.yuncheng.spcyApi.utils.WordPdfUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.io.File;

/**
 * @Description: OA附件资源库
 * @Author: lhb
 * @Date: 2023-08-11
 * @Version: V1.0
 */
@Service
public class OaFjzykEntityEntityServiceImpl extends BaseServiceImpl<OaFjzykEntityMapper, OaFjzykEntity> implements IOaFjzykEntityService {

    @Resource
    private OaLocalFileUploadUtils oaLocalFileUploadUtils;

    @Resource
    private OaBusinessConstant oaBusinessConstant;

    @Resource
    private WordPdfUtils oaWordPdfUtils;

    @Resource
    @Lazy
    private IOaFjzykEntityService oaFjzykService;


    /**
     * 生产oa文件pdf版
     */
    public void zltxwjToPdf(OaFjzykEntity oaFjzykEntity) {
        if (StringUtils.isNotBlank(oaFjzykEntity.getFpdfwjlj())) {
            oaLocalFileUploadUtils.deleteLocalFileUrlByZdy(oaFjzykEntity.getFpdfwjlj());
        }
        /**
         * 生产pdf文件版
         */
        String wordUrl = oaBusinessConstant.UPLOAD_FILE_OAFJ + oaFjzykEntity.getFwjlj();
        String nowDate = DateUtil.today();
        //创建目录
        String ml = "/" + OaCommonFlowConstant.OA_FILE_FJ + "/pdf/" + nowDate;
        File dir = new File(oaBusinessConstant.UPLOAD_FILE_OAFJ + ml);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        String pdfUrl = ml + "/" + oaFjzykEntity.getFmc() + ".pdf";
        oaWordPdfUtils.wordToPdf(wordUrl, oaBusinessConstant.UPLOAD_FILE_OAFJ + pdfUrl);

        oaFjzykEntity.setFpdfwjlj(pdfUrl);
        oaFjzykService.updateById(oaFjzykEntity);

    }

}
